const { validationResult } = require("express-validator");
const formatError = require("../utils/formatError");
const {
    listBlog,
    addBlog,
    detailBlog,
    findBlog,
    deleteBlog,
} = require("../model/blog");

// 列表
const ListBlog = async (req, res) => {
    const result = validationResult(req);
    if (!result.isEmpty()) {
        return res.status(400).send({
            code: 400,
            msg: "参数错误",
            data: formatError(result.array()),
        });
    }
    try {
        const author = req.params.id;
        let { page = 1, size = 10 } = req.query;
        size = Math.max(1, size);
        page = Math.max(1, page);
        const blogs = await listBlog(author, size, page);
        res.status(200).send({
            code: 200,
            msg: "获取成功",
            data: blogs,
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: "Internal Server Error",
            data: null,
        });
    }
};
// 添加博客
const AddBlog = async (req, res) => {
    const result = validationResult(req);
    if (!result.isEmpty()) {
        return res.status(400).send({
            code: 400,
            msg: "参数错误",
            data: formatError(result.array()),
        });
    }
    try {
        const { title, content } = req.body;
        const blog = await addBlog({
            title,
            content,
            author: req.session.user._id,
        });
        res.status(200).send({
            code: 200,
            msg: "博客发表成功",
            data: {
                id: blog._id,
            },
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: "Internal Server Error",
            data: null,
        });
    }
};

// 博客详情
const DetailBlog = async (req, res) => {
    const result = validationResult(req);
    if (!result.isEmpty()) {
        return res.status(401).send({
            code: 401,
            msg: "参数错误",
            data: formatError(result.array()),
        });
    }
    try {
        const { id } = req.params;
        const blog = await detailBlog(id);
        res.status(200).send({
            code: 200,
            msg: "获取成功",
            data: blog,
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: "博客获取失败",
            data: null,
        });
    }
};

// 删除博客
const DelBlog = async (req, res) => {
    const result = validationResult(req);
    if (!result.isEmpty()) {
        return res.status(401).send({
            code: 401,
            msg: "参数错误",
            data: formatError(result.array()),
        });
    }
    try {
        const { id } = req.params;
        const blog = await findBlog({ _id: id });
        if (!blog) {
            return res.status(401).send({
                code: 401,
                msg: "博客不存在",
                data: null,
            });
        }
        if (blog.author.toString() !== req.session.user._id) {
            return res.status(401).send({
                code: 401,
                msg: "没有权限",
                data: null,
            });
        }
        await deleteBlog(id);
        res.status(200).send({
            code: 200,
            msg: "删除成功",
            data: null,
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: "博客删除失败",
            data: null,
        });
    }
};

module.exports = { AddBlog, DelBlog, DetailBlog, ListBlog };
